package cn.neusoft.service.impl;

import cn.neusoft.mapper.FoodMapper;
import cn.neusoft.pojo.Food;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.FoodQuery;
import cn.neusoft.service.IFoodService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class FoodService implements IFoodService {

    @Autowired
    FoodMapper foodMapper;

    @Transactional
    @Override
    public ResultBean add(Food food) {

        if (food==null ||
                StringUtils.isEmpty(food.getFoodName())){
            return ResultBean.ERROR("请求参数为空");
        }
        int add = foodMapper.add(food);
        return add>0?ResultBean.SUCCESS(food.getFoodId()):ResultBean.ERROR("添加餐品失败");
    }

    @Transactional
    @Override
    public ResultBean update(Food food) {
        if (food==null ||
                food.getFoodId()==null || food.getFoodId()==0 ||
                StringUtils.isEmpty(food.getFoodName())){
            return ResultBean.ERROR("请求参数为空");
        }
        int update = foodMapper.update(food);
        return update>0?ResultBean.SUCCESS():ResultBean.ERROR("修改餐品失败");
    }

    @Transactional
    @Override
    public ResultBean delete(Integer id) {

        if (id==null || id==0){
            return ResultBean.ERROR("请求参数为空");
        }

        int delete = foodMapper.delete(id);
        return delete>0?ResultBean.SUCCESS():ResultBean.ERROR("删除餐品失败");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getById(Integer id) {

        if (id==null || id==0){
            return ResultBean.ERROR("请求参数为空");
        }
        Food food = foodMapper.getById(id);
        return food==null?ResultBean.ERROR("通过id查询餐品失败"):ResultBean.SUCCESS(food);
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getAll() {
        List<Food> foodList = foodMapper.getAll();
        return ResultBean.SUCCESS(foodList);
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getByPage(BaseQuery baseQuery) {

        if (baseQuery==null){
            baseQuery = new FoodQuery();
        }

        PageBean<Food> pageBean = new PageBean<>();

        int count = foodMapper.getCount(baseQuery);
        pageBean.setTotalRows(count);

        List<Food> foodList = foodMapper.getByPage(baseQuery);
        pageBean.setData(foodList);

        return ResultBean.SUCCESS(pageBean);
    }

    @Transactional
    @Override
    public ResultBean batchDel(List<Integer> ids) {

        if (ids==null || ids.size()==0){
            return ResultBean.ERROR("请求参数为空");
        }

        for (Integer id: ids) {
            int delete = foodMapper.delete(id);
            if (delete==0){
                throw new RuntimeException("删除餐品失败");
            }
        }

        return ResultBean.SUCCESS();
    }

    /*
    根据商家id查询餐品
     */
    @Override
    public ResultBean getByBusinessId(Integer businessId) {

        if (businessId==null || businessId==0){
            return ResultBean.ERROR("请求参数为空");
        }

        List<Food> foodList = foodMapper.getByBusinessId(businessId);

        return ResultBean.SUCCESS(foodList);
    }
}
